<?php
/**
 * 获取百度城市，写入到 es 中
 */
header("content-type:text/plain;charset=utf8");
require './include/CurlMulti.php';

$CurlMulti = new CurlMulti();
$CurlMulti->cache = array (
    'dir' => 'E:\clone\xiaoqu',
    'on' => true,
    'expire' => 3600 * 2400
);

// 获取城市列表
global $CITY_LIST;
$CITY_LIST = array();
$CurlMulti->add(array(
    'url' => 'http://map.baidu.com/?qt=s&wd=小区',
), function ($r, $args){
    global $CITY_LIST;
    $contents = json_decode($r['content'], true);
    $nn = 0;
    foreach ($contents['content'] as $city) {
        $CITY_LIST[$city['code']] = array(
            'name' => $city['name'],
            'code' => $city['code'],
            'geo' => $city['geo'],
        );
    }

    foreach ($contents['more_city'] as $province => $cityList) {
        foreach ($cityList['city'] as $city) {
            $CITY_LIST[$city['code']] = array(
                'name' => $city['name'],
                'code' => $city['code'],
                'geo' => $city['geo'],
            );
        }
    }
}, function ($r, $args){
    var_dump($r, $args);
});
$CurlMulti->start();


echo 'line:', __LINE__ , ' 城市数量:' , count($CITY_LIST), "\n";

// 存储数据

$index = 'baidu_city';
$type = 'city';

$es_bulk = $conf['es']['hosts'].'/'.$index.'/'.$type.'/_bulk';
$es_url = $conf['es']['hosts'].'/'.$index.'/'.$type.'/';
$request = array();
$times = 0;

$body = '';
foreach($CITY_LIST as $key => $item){
    echo $key, "\n";
    $_index = array("index"=>array("_id"=>sprintf("%u",($key))));
    $body .= json_encode($_index) . "\n";
    $body .=  json_encode($item) . "\n";
    $times++;
    if (($times+1) % 1000 == 0) {
        $multi_request[] = $body;
        $body = "";
    }
}
$multi_request[] = $body;
global $request;
$request = 0;
try {
    foreach ($multi_request as $body) {
        $res = fly_curl($es_bulk,$body);
        $request++;
    }
} catch(Exception $e) {
    echo '<pre>';
    var_dump($e->getCode(), $e->getLine(), $e->getMessage());
    echo '</pre>';
}

echo $request, "\n";
